import asyncio
import pprint
import random
from random import randint


def mygen(alist: list):
    while len(alist) > 0:
        c = randint(0, len(alist) - 1)
        yield alist.pop(c)


a = ["aa", "bb", "cc", "dd"]
c = mygen(a)
print(c)


def gen():
    value = 0
    while True:
        receive = yield value
        if receive == 'e':
            break
        value = 'got: %s' % receive


g = gen()
print(g.__next__())
print(next(g))
print(g.send(None))
print(g.send('hello'))
print(g.send(12345))
print(next(g))
print(next(g))
print(next(g))


def g1():
    yield range(5)


def g2():
    yield from range(5)  # yield from iterable本质上等于for item in iterable: yield item的缩写版


def g3():
    for item in range(5):
        yield item


it1 = g1()
it2 = g2()
it3 = g3()
for x in it1:
    print(x)
for x in it2:
    print(x)
for x in it3:
    print(x)


def fab(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        print(b)
        a, b = b, a + b
        n = n + 1


def f_wrapper(fun_iterable):
    print('start')
    for item in fun_iterable:
        yield item
    print('end')


def f_wrapper2(fun_iterable):
    print('start')
    yield from fun_iterable
    print('end')


wrap = f_wrapper2(fab(5))
for i in wrap:
    print(i, end=' ')


# 在 Python 3.8 及更高版本中，@asyncio.coroutine 已被弃用并移除，取而代之的是使用 async def 来定义协程函数。
# 包含@asyncio.coroutine 装饰器的将从Python3.11中删除，因此asyncio 模块没有@asyncio.coroutine 装饰符，错误是这样写的：
# 注释:对基于生成器的协同服务的支持被废弃，并在Python3.11中被删除。
# @asyncio.coroutine
async def smart_fib(n):
    index = 0
    a = 0
    b = 1
    while index < n:
        sleep_secs = random.uniform(0, 0.2)
        # yield from asyncio.sleep(sleep_secs)
        await asyncio.sleep(sleep_secs)
        print('Smart one think {} secs to get {}'.format(sleep_secs, b))
        a, b = b, a + b
        index += 1


# @asyncio.coroutine
async def stupid_fib(n):
    index = 0
    a = 0
    b = 1
    while index < n:
        sleep_secs = random.uniform(0, 0.4)
        # yield from asyncio.sleep(sleep_secs)
        await asyncio.sleep(sleep_secs)
        print('Stupid one think {} secs to get {}'.format(sleep_secs, b))
        a, b = b, a + b
        index += 1


async def mygen2(alist):
    while len(alist) > 0:
        c = randint(0, len(alist) - 1)
        yield alist.pop(c)


a = ["a", "b", "c"]
c = mygen2(a)
print(c)


async def mygen3(alist):
    while len(alist) > 0:
        c = random.randint(0, len(alist) - 1)
        print(alist.pop(c))
        await asyncio.sleep(1)


strlist = ['ss', 'dd', 'gg']
intlist = [1, 2, 5, 6]
c1 = mygen3(strlist)
c2 = mygen3(intlist)
# print(c1)

pprint.pprint('#' * 50)


def count_down(n):
    while n > 0:
        print('>>yield n:%d' % n)
        newn = yield n
        print('newn', newn)
        if newn:
            print('if')
            n = newn
            print('n =', n)
        else:
            print('>>n-=1:%d' % n)
            n -= 1


cd = count_down(5)
for i in cd:
    print(i, ',')
    if i == 5:
        cd.send(3)




# if __name__ == '__main__':
# loop = asyncio.get_event_loop()
# tasks = [
#     smart_fib(10),
#     stupid_fib(10),
# ]
# loop.run_until_complete(asyncio.wait(tasks))
# print('All fib finished.')
# loop.close()
